<script setup lang="ts">
	import { useCorps } from "./productUtils/hook";
	import Refresh from "@iconify-icons/ep/refresh";
	import AddFill from "@iconify-icons/ri/add-circle-line";
	import { PureTableBar } from "@/components/RePureTableBar";
	import { useRenderIcon } from "@/components/ReIcon/src/hooks";
	import { ref, onMounted } from "vue";
	const {
		form,
		loading,
		columns,
		dataList,
		pagination,
		isShow,
		onSearch,
		resetForm,
		openDialog,
		handleSizeChange,
		handleCurrentChange,
		deviceDetection,
    projectSelect,
    sortChange,
    purchaseOrderSelect,
    getPurchaseOrder,
    companySelect
	} = useCorps();
	const formRef = ref();
  onMounted(async()=> {

  })
  //合计
  const getSummaries = (param) => {
    const { columns, data } = param;
    const sums = [];
    columns.forEach((column, index) => {
      if (index === 0) {
        sums[index] = '合计';
        return;
      }
      const values = data.map(item => Number(item[column.property]));
      if (!values.every(value => isNaN(value))) {
        sums[index] = values.reduce((prev, curr) => {
          const value = Number(curr);
          if(column.property === 'number') {
            return prev + curr;
          } else if(column.property === 'amount') {
            return prev + curr;
          } else {
            return '--'
          }
        }, 0);
        // sums[index] += '';
      } else {
        sums[index] = '--';
      }
    });
    return sums;
  };
</script>

<template>
	<div class="main">
		<el-form ref="formRef" :inline="true" :model="form"
			class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto">
      <el-form-item label="所属公司:">
        <el-select v-model="form.companyId" class="!w-[180px]" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in companySelect"
            :key="item.companyId"
            :value="item.companyId"
            :label="item.companyName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库单:">
        <el-select v-model="form.purchaseOrderId" class="!w-[180px]" filterable clearable placeholder="请选择" :filter-method="getPurchaseOrder">
          <el-option
            v-for="item in purchaseOrderSelect"
            :key="item.purchaseOrderId"
            :value="item.purchaseOrderId"
            :label="item.orderNo"
          />
        </el-select>
      </el-form-item>
			<el-form-item label="产品名称:">
				<el-input v-model="form.productName" class="!w-[180px]" clearable placeholder="请输入" />
			</el-form-item>
      <el-form-item label="产品编号:">
      	<el-input v-model="form.productNo" class="!w-[180px]" clearable placeholder="请输入" />
      </el-form-item>
      <el-form-item label="项目:">
        <el-select v-model="form.projectId" class="!w-[180px]" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in projectSelect"
            :key="item.projectId"
            :value="item.projectId"
            :label="item.projectName"
          />
        </el-select>
      </el-form-item>
     <!-- <el-form-item label="仓库:">
      	<el-input v-model="form.storeName" class="!w-[180px]" clearable placeholder="请输入" />
      </el-form-item> -->
      <el-form-item label="关键词:">
      	<el-input v-model="form.keyword" class="!w-[180px]" clearable placeholder="请输入" />
      </el-form-item>
      <el-form-item label="创建时间:">
        <el-date-picker v-model="form.dateRange" type="daterange" placeholder="请选择" clearable style="width: 300px" format="YYYY-MM-DD"
          value-format="YYYY-MM-DD" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
			<el-form-item>
				<el-button type="primary" :icon="useRenderIcon('ri:search-line')" :loading="loading" @click="onSearch">
					搜索
				</el-button>
				<el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
					重置
				</el-button>
			</el-form-item>
		</el-form>

		<div ref="contentRef" :class="['flex', deviceDetection() ? 'flex-wrap' : '']">
			<PureTableBar :class="[isShow && !deviceDetection() ? '!w-[60vw]' : 'w-full']"
				style="transition: width 220ms cubic-bezier(0.4, 0, 0.2, 1)" title=" " :columns="columns"
				@refresh="onSearch">
				<template v-slot="{ size, dynamicColumns }">
					<pure-table ref="tableRef" showOverflowTooltip table-layout="auto"
						:loading="loading" :size="size" adaptive :adaptiveConfig="{ offsetBottom: 108 }" :summary-method="getSummaries" show-summary
						:data="dataList" :columns="dynamicColumns" :pagination="pagination" @sort-change="sortChange"
						:paginationSmall="size === 'small' ? true : false" :header-cell-style="{background: 'var(--el-fill-color-light)',
						color: 'var(--el-text-color-primary)'}" @page-size-change="handleSizeChange" @page-current-change="handleCurrentChange">
            <template #operation=" {row} ">
              <el-button type="primary" link @click="openDialog('详情', row)">详情</el-button>
            </template>
					</pure-table>
				</template>
			</PureTableBar>
		</div>
	</div>
</template>

<style scoped lang="scss">
	.main-content {
		margin: 10px 10px 0 !important;
	}

</style>
